package com.itbaizhan.shopping_cart_service.service;

import com.itbaizhan.shopping_common.pojo.CartGoods;
import com.itbaizhan.shopping_common.service.CartService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author spf
 * @date 2024/1/14
 * @time 14:21
 */

// 购物车实现类
@Service
@Component
@Transactional
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @RabbitListener(queues = "sync_cart_queue")
    public void listenSyncQueue(CartGoods cartGoods){
        refreshCartGoods(cartGoods);
    }

    @RabbitListener(queues = "del_cart_queue")
    public void listenDelQueue(CartGoods cartGoods){
        deleteCart(cartGoods);
    }
    @Override
    public void addCart(Long userId, CartGoods cartGoods) {
        // 现根据userid查询购物车集合
        List<CartGoods> cartList = findCartList(userId);
        // 遍历购物车，如果有就把对应的数量加上所加数量
        for (CartGoods cartGoods1 : cartList) {
            if (cartGoods1.getGoodId().equals(cartGoods.getGoodId())){
                cartGoods1.setNum(cartGoods1.getNum() + cartGoods.getNum());
                redisTemplate.boundHashOps("cartList").put(userId, cartList);
                return;
            }
        }

        // 如果没有就把商品数量添加进去
        cartList.add(cartGoods);
        redisTemplate.boundHashOps("cartList").put(userId, cartList);
    }

    @Override
    public void handleCart(Long userId, Long goodId, Integer num) {
        // 查询用户的购物车
        List<CartGoods> cartList = findCartList(userId);
        for (CartGoods cartGoods : cartList) {
            if (cartGoods.getGoodId().equals(goodId)){
                cartGoods.setNum(num);
                break;
            }
        }
        // 将新的购物车列表保存到redis中
        redisTemplate.boundHashOps("cartList").put(userId, cartList);
    }

    @Override
    public void deleteCartOption(Long userId, Long goodId) {
        // 查询用户的购物车
        List<CartGoods> cartList = findCartList(userId);
        for (CartGoods cartGoods : cartList) {
            if (cartGoods.getGoodId().equals(goodId)){
                cartList.remove(cartGoods);
                break;
            }
        }
        // 将新的购物车列表保存到redis中
        redisTemplate.boundHashOps("cartList").put(userId, cartList);
    }

    @Override
    public List<CartGoods> findCartList(Long userId) {
        BoundHashOperations boundedHashOps = redisTemplate.boundHashOps("cartList");
        Object cartList= boundedHashOps.get(userId);
        if (cartList == null){
            return new ArrayList<CartGoods>();
        }else {
            return (List<CartGoods>) cartList;
        }
    }

    @Override
    public void refreshCartGoods(CartGoods cartGoods) {
        // 查询所有用户的所有购物车商品
        BoundHashOperations boundedHashOps = redisTemplate.boundHashOps("cartList");
        Map<String, List<CartGoods>> allCartGoods = boundedHashOps.entries();
        Set<Map.Entry<String, List<CartGoods>>> entries = allCartGoods.entrySet();

        // 遍历所有键值对，遍历所有用户的购物车
        for (Map.Entry<String, List<CartGoods>> entry : entries) {
            List<CartGoods> cartGoods1 = entry.getValue();
            // 遍历一个用户的购物车
            for (CartGoods goods : cartGoods1) {
                // 如果该商品是被修改的商品，则修改商品
                if (goods.getGoodId().equals(cartGoods.getGoodId())){
                    goods.setGoodsName(cartGoods.getGoodsName());
                    goods.setHeaderPic(cartGoods.getHeaderPic());
                    goods.setPrice(cartGoods.getPrice());
                    break;
                }
            }
        }
        // 将修改的cartGoods重新放到redis中
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(allCartGoods);
    }

    @Override
    public void deleteCart(CartGoods cartGoods) {
        // 获取所有用户的购物车商品
        BoundHashOperations boundedHashOps = redisTemplate.boundHashOps("carftList");
        Map<String, List<CartGoods>> allCartGoods = boundedHashOps.entries();
        Set<Map.Entry<String, List<CartGoods>>> entries = allCartGoods.entrySet();
        // 遍历所有用户的购物车
        for (Map.Entry<String, List<CartGoods>> entry : entries) {
            List<CartGoods> cartGoods1 = entry.getValue();
            for (CartGoods goods : cartGoods1) {
                // 如果管理员下架商品则删除每个用户购物车关于此商品的
                if (goods.getGoodId().equals(cartGoods.getGoodId())){
                    cartGoods1.remove(goods);
                    break;
                }
            }
        }
        // 重新将pam中的数据放入redis
        redisTemplate.delete("cartList");
        redisTemplate.boundHashOps("cartList").putAll(allCartGoods);
    }
}
